﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_DangKyLopHop : System.Web.UI.Page
{
    _dbDataContext _db = new _dbDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    private void BindGrid()
    {
        var list = from p in _db.tblDaoTaos
                   select new
                   {
                       ID = p.ID,
                       MaLop = p.TenLopID,
                       TuNgay = string.Format("{0:dd/MM/yyyy}", p.ThoiGianDaoTaoTu),
                       DenNgay = string.Format("{0:dd/MM/yyyy}", p.ThoiGianDaoTaoDen),
                       NgayKhaiGiang = string.Format("{0:dd/MM/yyyy}", p.NgayKhaiGiang),
                       NgayHetHanDangKy = string.Format("{0:dd/MM/yyyy}", p.NgayHetHanDangKy),
                       GhiChu = p.GhiChu,
                       TenLop = p.TenLopID
                   };
        griddslop.DataSource = list.ToList();
        griddslop.DataBind();
    }

    private void BindListManagerToGrid()
    {
        var list = from p in _db.tblCongTacDoans
                   where p.DonViTrucThuoc.Trim() == MemberShip_ASP.GetMaDonViFromUserName(this.Page.User.Identity.Name)
                   select new
                   {
                       MaDoanVien=p.MaDoanVien,
                       TenDoanVien=p.tblDoanVien.HoTen.Trim(),
                       DonViTrucThuoc=_db.tblCoSoDoans.Single(r=>r.MaDonVi.Trim()==_db.tblDoanViens.Single(a=>a.MaDonVien.Trim()==p.MaDoanVien).TrucThuoc.Trim()).TenDonVi.Trim(),
                       DonViCongTac = MemberShip_ASP.GetMaDonViFromUserName(this.Page.User.Identity.Name),
                       ChucVu=p.tblDanhMuc2.TenDanhMuc.Trim(),
                       ChucVuDoanID=p.ChucVuHienTai,
                       DaDangKy=_db.tblDangKyLopHocs.Where(r=>r.MaDoanVien.Trim()==p.MaDoanVien && r.MaLopID==int.Parse(txtMaLopID.Value)).Count()==0?false:true
                   };
        gridmanager.DataSource = list.ToList();
        gridmanager.DataBind();
    }
    protected void griddslop_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "CommandDangKy")
        {
            string ngay = ((Label)griddslop.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lblNgayHetHan")).Text;
            int ngayhethan = CatalogAccess.DoiNgaySangSoTinhTu1930(CatalogAccess.ConvertToDate(ngay));
            int ngayhientai = CatalogAccess.DoiNgaySangSoTinhTu1930(DateTime.Now);
            if (ngayhethan > ngayhientai)
            {
                SecurityLib.ToolsMore.ShowPopUpMsg("Đã hết hạn đăng ký", this.Page);
            }
            else
            {
                BindDataToControls(e);
                pncontent.Visible = false;
                pnDangky.Visible = true;
                BindListManagerToGrid();
            }
        }
    }
    protected void btnclose_Click(object sender, EventArgs e)
    {
        pnDangky.Visible = false;
        pncontent.Visible = true;
    }
    private void BindDataToControls(GridViewCommandEventArgs e)
    {
        txtTenLop.Text = ((DropDownList)griddslop.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("drlXepLoai")).SelectedItem.Text;
        txttungay.Text = ((Label)griddslop.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lblTuNgay")).Text;
        txtdenngay.Text = ((Label)griddslop.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lblDenNgay")).Text;
        txtNgayKhaiGiang.Text = ((Label)griddslop.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lblNgayKhaiGiang")).Text;
        txtNgayHetHanDangKy.Text = ((Label)griddslop.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lblNgayHetHan")).Text;
        txtGhiChu.Text = ((Label)griddslop.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lblGhiChu")).Text;
        txtMaLopID.Value = griddslop.DataKeys[int.Parse(e.CommandArgument.ToString())].Value.ToString();
    }
    protected void chk_DangKy_CheckedChanged(object sender, EventArgs e)
    {
        int selRowIndex = ((GridViewRow)(((CheckBox)sender).Parent.Parent)).RowIndex;
        string madoanvien = gridmanager.DataKeys[selRowIndex].Value.ToString();
        CheckBox cb = (CheckBox)gridmanager.Rows[selRowIndex].FindControl("chk_DangKy");
        if (cb.Checked == true)
        {
            tblDangKyLopHoc _tb = new tblDangKyLopHoc();
            _tb.MaLopID = int.Parse(txtMaLopID.Value);
            _tb.MaDoanVien = madoanvien;
            _tb.DonViVongTac = MemberShip_ASP.GetMaDonViFromUserName(this.Page.User.Identity.Name);
            _tb.DonViTrucThuoc = ((Label)gridmanager.Rows[selRowIndex].FindControl("lbldonvicongtac")).Text;
            _tb.NgayDangKy = CatalogAccess.ConvertToDate(DateTime.Now.ToString());
            _tb.ChucVuDoanID = int.Parse(((Label)gridmanager.Rows[selRowIndex].FindControl("lblChucVuDoanID")).Text);
            try
            {
                _db.tblDangKyLopHocs.InsertOnSubmit(_tb);
                _db.SubmitChanges();
            }
            catch { }
        }
        else
        {
            int chucvudoanid = int.Parse(((Label)gridmanager.Rows[selRowIndex].FindControl("lblChucVuDoanID")).Text);
            var list = _db.tblDangKyLopHocs.Where(r => r.MaDoanVien.Trim() == madoanvien && r.MaLopID == int.Parse(txtMaLopID.Value) && r.ChucVuDoanID==chucvudoanid);
            try
            {
                _db.tblDangKyLopHocs.DeleteAllOnSubmit(list);
                _db.SubmitChanges();
            }
            catch { }
        }
    }
}